<template>
<div>
    <h2 style="text-align: center;">添加理财信息</h2>
    <table class="table-bordered" border="1">
        <tr>
            <td>理财产品名称</td>
            <td>
                <input type="text" v-model="info.F_Name" placeholder="请输入理财产品名称">
            </td>
        </tr>

        <tr>
            <td>年收益</td>
            <td>
                <input type="text" v-model="info.F_APY" placeholder="请输入年收益">
            </td>
        </tr>

        <tr>
            <td>最低金额</td>
            <td>
                <input type="text" v-model="info.F_MinMoney" placeholder="请输入最低金额">
            </td>
        </tr>

        <tr>
            <td>理财利息</td>
            <td>
                <select v-model="info.I_Id">
                    <option value="0">--请选择--</option>
                    <option v-for="item in SelInfo" :value="item.I_Id">{{ item.I_Year }}年利息{{ item.I_Interest }}%</option>
                </select>
            </td>
        </tr>

        <tr>
            <td>风险等级</td>
            <td>
                <input type="radio" value="0" name="Flevel" v-model="info.F_Level" checked>低风险
                <input type="radio" value="1" name="Flevel" v-model="info.F_Level">中风险
                <input type="radio" value="2" name="Flevel" v-model="info.F_Level">高风险
            </td>
        </tr>

        <tr>
            <td colspan="2" style="text-align: center;">
                <input type="button" value="保存" class="btn btn-success" @click="SaveFinan">
            </td>
        </tr>
    </table>
</div>
</template>

<script setup lang="ts">
import axios from 'axios';
import {ref,onMounted} from 'vue';
import { useRouter } from 'vue-router';

//创建路由对象实例
const router=useRouter();

//创建响应式数据
const info=ref({
    F_Name:"",
    F_APY:"",
    F_MinMoney:"",
    I_Id:0,
    F_Level:""
})

//创建响应式数据 用于绑定下拉框
const SelInfo=ref([{
    I_Id:0,
    I_Year:0,
    I_Interest:0
}])

//创建生命周期函数钩
onMounted(()=>{
    LoadSelete()
})

//完成下拉框加载数据
const LoadSelete=()=>{
    axios.get("https://localhost:44321/api/Finans/ShowInterest")
    .then(res=>{
        SelInfo.value=res.data
    })
}

//完成保存理财信息
const SaveFinan=()=>{
    //验证非空

    axios.post("https://localhost:44321/api/Finans/AddFinanInfo",
        {
            F_Name:info.value.F_Name,
            F_APY:info.value.F_APY,
            F_MinMoney:info.value.F_MinMoney,
            F_Level:info.value.F_Level,
            I_Id:info.value.I_Id
        }
    )
    .then(res=>{
        if(res.data>0)
        {
            alert('理财信息添加成功');
            router.push("/ShowFinaninfo")
        }
        else if(res.data==-1)
        {
            alert('理财信息名称不允许重复,请重新输入');
        }
        else
        {
            alert('理财信息添加失败！！！');
        }
    })
}

</script>